package com.zhy.generator.codegen.mybatis.javamapper;

import com.zhy.generator.codegen.api.CommentGenerator;
import com.zhy.generator.codegen.dom.java.CompilationUnit;
import com.zhy.generator.codegen.dom.java.FullyQualifiedJavaType;
import com.zhy.generator.codegen.dom.java.Interface;
import com.zhy.generator.codegen.dom.java.JavaVisibility;
import com.zhy.generator.codegen.mybatis.AbstractJavaClientGenerator;
import com.zhy.generator.codegen.mybatis.AbstractXmlGenerator;
import com.zhy.generator.codegen.mybatis.xmlmapper.XMLMapperGenerator;
import com.zhy.generator.config.ConfigurationConstant;
import com.zhy.generator.config.PropertyRegistry;

import java.util.ArrayList;
import java.util.List;

import static cn.hutool.core.util.StrUtil.isBlank;
import static cn.hutool.core.util.StrUtil.isNotBlank;
import static com.zhy.generator.codegen.util.Messages.getString;

/**
 * @author 源
 */
public class JavaMapperGenerator extends AbstractJavaClientGenerator {
    public JavaMapperGenerator() {
        super(true);
    }

    public JavaMapperGenerator(boolean requiresXMLGenerator) {
        super(requiresXMLGenerator);
    }

    @Override
    public AbstractXmlGenerator getMatchedXMLGenerator() {
        return new XMLMapperGenerator();
    }

    @Override
    public List<CompilationUnit> getCompilationUnits() {
        progressCallback.startTask(getString("Progress.10", introspectedTable.getFullyQualifiedTable()));
        CommentGenerator commentGenerator = context.getCommentGenerator();

        FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());
        Interface interfaze = new Interface(type);
        interfaze.setVisibility(JavaVisibility.PUBLIC);
        commentGenerator.addJavaFileComment(interfaze);

        String rootInterface = introspectedTable.getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
        if (isBlank(rootInterface)) {
            rootInterface = context.getConfiguration(ConfigurationConstant.CONFIGURATION_MAPPER).getRootInterface();
        }

        if (isNotBlank(rootInterface)) {

            FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(rootInterface);
            //范型类
            FullyQualifiedJavaType basetype = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
            fqjt.addTypeArgument(basetype);
            interfaze.addImportedType(basetype);

            //继承接口类
            interfaze.addSuperInterface(fqjt);
            interfaze.addImportedType(fqjt);
        }

        interfaze.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Repository")); //$NON-NLS-1$
        interfaze.addAnnotation("@Repository");
        interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper")); //$NON-NLS-1$
        interfaze.addAnnotation("@Mapper");

        //扩展
        List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
        if (context.getPlugins().clientGenerated(interfaze, null, introspectedTable)) {
            answer.add(interfaze);
        }

        List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits();
        if (extraCompilationUnits != null) {
            answer.addAll(extraCompilationUnits);
        }

        return answer;
    }

    public List<CompilationUnit> getExtraCompilationUnits() {
        return null;
    }
}
